/*
 * CreateArmyDialog.java
 *
 * Created on 20 de julio de 2008, 17:52
 */

package org.perfectday.dashboard.gui;

import java.awt.GraphicsEnvironment;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.perfectday.dashboard.gui.model.ArmyTableModel;
import org.perfectday.gamebuilder.model.BattleDescription;
import org.perfectday.gamebuilder.model.MiniDescription;
import org.perfectday.logicengine.model.minis.MiniFactory;
import org.perfectday.logicengine.model.minis.MiniLevel;

/**
 *
 * @author  Miguel Angel Lopez Montellano (alakat@gmail.com)
 */
public class CreateArmyDialog extends javax.swing.JDialog {

    private ArmyTableModel model;
    private static final NumberFormat nf = new DecimalFormat();
    private static final Logger logger = Logger.getLogger(CreateArmyDialog.class);
    /** Creates new form CreateArmyDialog
     * @param parent
     * @param modal
     * @param bd 
     */
    public CreateArmyDialog(java.awt.Frame parent, boolean modal,BattleDescription bd) {
        super(parent, modal);
        initComponents();
        model = new ArmyTableModel();
        this.lEnemy.setText(bd.getEnemy());
        this.lMission.setText(bd.getMission().toString());
        this.lMaxBattlePoint.setText(""+bd.getPoint());
        this.armyTab.setModel(model);
        this.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint());
        this.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint());
        for (MiniDescription miniDescription : bd.getMinis()) {
            this.cbMinis.addItem(miniDescription);
        }
        for (int i = 0; i < MiniLevel.values().length; i++) {
            MiniLevel lvl = MiniLevel.values()[i];
            this.cbLevel.addItem(lvl);
            
        }
        
    }

    public List<MiniDescription> getArmyList() {
        return model.generateArmyList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        armyTab = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lEnemy = new javax.swing.JLabel();
        lMission = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lMaxBattlePoint = new javax.swing.JLabel();
        cbMinis = new javax.swing.JComboBox();
        cbLevel = new javax.swing.JComboBox();
        lPoint = new javax.swing.JLabel();
        bAddMini = new javax.swing.JButton();
        bDeleteMini = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        lTotalPoint = new javax.swing.JLabel();
        bAceptar = new javax.swing.JButton();
        cbColor = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cree el ejecercito para la partida");
        setMinimumSize(new java.awt.Dimension(10, 10));

        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(460, 422));

        armyTab.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(armyTab);

        jLabel1.setText("Contrincante:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Misión:");
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        lEnemy.setText("jLabel3");

        lMission.setText("jLabel3");

        jLabel3.setText("Ejercito:");

        lMaxBattlePoint.setText("jLabel4");

        cbMinis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbMinisActionPerformed(evt);
            }
        });

        cbLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbLevelActionPerformed(evt);
            }
        });

        lPoint.setText("28.8");

        bAddMini.setText("Incluir en Ejército");
        bAddMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddMiniActionPerformed(evt);
            }
        });

        bDeleteMini.setText("Eliminar mini");
        bDeleteMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bDeleteMiniActionPerformed(evt);
            }
        });

        jLabel4.setText("Coste:");

        lTotalPoint.setText("0.00");

        bAceptar.setText("Aceptar");
        bAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAceptarActionPerformed(evt);
            }
        });

        cbColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Negro", "Rojo", "Azul", "Blanco" }));
        cbColor.setInheritsPopupMenu(true);

        jLabel6.setText("Color del equipo:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bDeleteMini)
                                .addGap(119, 119, 119)
                                .addComponent(bAceptar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lTotalPoint, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbMinis, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lPoint)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bAddMini, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbColor, 0, 267, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lMaxBattlePoint, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lMission)
                                .addComponent(lEnemy)))
                        .addGap(192, 192, 192))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lEnemy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lMission))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lMaxBattlePoint))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAddMini)
                    .addComponent(lPoint)
                    .addComponent(cbLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbMinis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lTotalPoint)
                    .addComponent(bDeleteMini)
                    .addComponent(bAceptar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void bAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAceptarActionPerformed
// TODO add your handling code here:
    
    double maxCost = Double.parseDouble(this.lMaxBattlePoint.getText());
    if(maxCost< this.model.getTotalCost())
        JOptionPane.showMessageDialog(this, "No se puede crear, el ejercito tiene demasiado coste","Aviso",JOptionPane.WARNING_MESSAGE);
    else{
        this.dispose();
    }
}//GEN-LAST:event_bAceptarActionPerformed

private void bAddMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddMiniActionPerformed
    if(this.cbLevel.getSelectedItem()!=null &&
            this.cbMinis.getSelectedItem()!=null){
        MiniDescription mini = (MiniDescription) this.cbMinis.getSelectedItem();
        MiniLevel level  = (MiniLevel) this.cbLevel.getSelectedItem();
        mini.setLevel(level);
        this.model.addRow(mini, level,mini.getCost() * MiniFactory.getInstance().getMiniCost(level));    
     //   this.armyTab.setModel(this.model);
        this.armyTab.updateUI();
        this.lTotalPoint.setText(nf.format(this.model.getTotalCost()));    
        this.pack();
    }
}//GEN-LAST:event_bAddMiniActionPerformed

private void cbMinisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbMinisActionPerformed
    if(this.cbLevel.getSelectedItem()!=null &&
            this.cbMinis.getSelectedItem()!=null){
        MiniDescription mini = (MiniDescription) this.cbMinis.getSelectedItem();
        MiniLevel level  = (MiniLevel) this.cbLevel.getSelectedItem();
        this.lPoint.setText(nf.format(mini.getCost() * MiniFactory.getInstance().getMiniCost(level)));
    }
}//GEN-LAST:event_cbMinisActionPerformed

private void cbLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLevelActionPerformed
    if(this.cbLevel.getSelectedItem()!=null &&
            this.cbMinis.getSelectedItem()!=null){
        MiniDescription mini = (MiniDescription) this.cbMinis.getSelectedItem();
        MiniLevel level  = (MiniLevel) this.cbLevel.getSelectedItem();
        this.lPoint.setText(nf.format(mini.getCost() * MiniFactory.getInstance().getMiniCost(level)));
    }
}//GEN-LAST:event_cbLevelActionPerformed

private void bDeleteMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDeleteMiniActionPerformed
    int idx = this.armyTab.getSelectedRow();
    if(idx>-1){
        this.model.remove(idx);
        this.lTotalPoint.setText(nf.format(this.model.getTotalCost()));    
        this.pack();
    }
    this.armyTab.updateUI();
}//GEN-LAST:event_bDeleteMiniActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable armyTab;
    private javax.swing.JButton bAceptar;
    private javax.swing.JButton bAddMini;
    private javax.swing.JButton bDeleteMini;
    private javax.swing.JComboBox cbColor;
    private javax.swing.JComboBox cbLevel;
    private javax.swing.JComboBox cbMinis;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lEnemy;
    private javax.swing.JLabel lMaxBattlePoint;
    private javax.swing.JLabel lMission;
    private javax.swing.JLabel lPoint;
    private javax.swing.JLabel lTotalPoint;
    // End of variables declaration//GEN-END:variables

}
